package com.axc.gallery2.view;


import java.lang.Thread.State;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

public abstract class GalleryProgressDialog extends ProgressDialog  {

	public GalleryProgressDialog(Context context) {
		super(context);
		//s设置样式及显示信息
		this.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		this.setMessage("Processing...");
	}
	//要执行的耗时函数
	public abstract void  setRun();
	//线程对象
	Thread thread =new Thread(){

		@Override
		public void run() {
			try {
				setRun();				
			} catch (Exception e) {
				e.printStackTrace();
			}
			finally{
			handler.sendEmptyMessage(0);
			}
		}

	};
	//接收数据
	Handler  handler = new Handler() {
		public void handleMessage(Message msg) {
			GalleryProgressDialog.this.dismiss();
		}
	};
	//显示对话框并执行耗时函数
	public void showGalleryProgressDialog() {
		this.show();
		thread.start();
	}

	public void stop() {
		thread=null;
	}

	public void setHandler(Handler handler) {
		this.handler = handler;
	}
	@Override
	public void dismiss() {
		super.dismiss();
		if(thread.getState()!= State.TERMINATED){
			stop();
		}
	}
}

